package com.meihua.shellapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meihua.shellapp.R;


public class ToastDialog extends Dialog {

    private String con;//dialog内容
    private Context context;
    private OnDialogToastListener onDialogToastListener;

    public ToastDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ToastDialog(@NonNull Context context, @StyleRes int themeResId, String con) {
        super(context, themeResId);
        this.context = context;
        this.con = con;
    }

    protected ToastDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    public OnDialogToastListener getOnDialogToastListener() {
        return onDialogToastListener;
    }

    public void setOnDialogToastListener(OnDialogToastListener onDialogToastListener) {
        this.onDialogToastListener = onDialogToastListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        //以view的方式引入，然后回调activity方法，setContentView，实现自定义布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_toast, null);
        setContentView(view);

        TextView btnCancle = (TextView) view.findViewById(R.id.dialog_toast_ok);
        TextView tv_con = (TextView) view.findViewById(R.id.dialog_toast_et);
        tv_con.setText(con);
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDialogToastListener.okClick();
            }
        });

        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public interface OnDialogToastListener {
        void okClick();
    }

}
